package com.peach.springbootdemo.demo.web.upload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author peacher
 * @date 2019-08-01
 */
@RequestMapping("upload")
@Controller
public class UploadFileController {

    /**
     * UPLOADED_FOLDER 文件本地存储地址
     */
    private final String UPLOADED_FOLDER = "C:\\Users\\peach\\Desktop\\image\\temp\\";

    /**
     * 浏览器访问 http://127.0.0.1:8080/upload/file
     * @return
     */
    @GetMapping("/file")
    public String index() {
        return "upload/uploadFile";
    }

    @PostMapping("/uploadFile")
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {

        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:uploadResult";
        }
        try {
            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            // UPLOADED_FOLDER 文件本地存储地址
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);

            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:uploadResult";
    }


    /**
     * 浏览器访问 http://127.0.0.1:8080/upload/file/more
     * @return
     */
    @GetMapping("/file/more")
    public String uploadMore() {
        return "upload/uploadFileMore";
    }


    @PostMapping("/file/uploadFileMore")
    public String moreFileUpload(@RequestParam("file") MultipartFile[] files,
                                 RedirectAttributes redirectAttributes) {
        if (files.length==0) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:/upload/uploadResult";
        }
        for(MultipartFile file:files){
            try {
                byte[] bytes = file.getBytes();
                Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
                Files.write(path, bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        redirectAttributes.addFlashAttribute("message", "You successfully uploaded all");
        return "redirect:/upload/uploadResult";
    }


    /**
     * 上传结果页面
     * @return
     */
    @RequestMapping("/uploadResult")
    public String uploadResult(){
        return "upload/uploadResult";
    }

}
